<?php 
require_once("soap/nusoap.php");
$ns="http://store8x.com/webser/";
//$ns="http://localhost/vfun/";
$server = new soap_server();  
$server->configureWSDL('Service',$ns);
$server->wsdl->schemaTargetNamespace=$ns;  
$server->soap_defencoding = 'UTF-8'; 
$server->register('receiveMO',
	array(	'commandcode' => 'xsd:string',  //EX: WF, WORLD
    		'serviceid' => 'xsd:string', // Số dịch vụ (6x76)
    		'userid'=> 'xsd:string', //84902171891
    		'message'=> 'xsd:string', //ACC pass,KD nje
    		'requestid'=> 'xsd:string',//ID MO: randomcode: 298963647
    		'username'=> 'xsd:string', //authentication
    		'password'=> 'xsd:string'),//authentication	
	 array('result' => 'xsd:string'),  
		    $ns,                                                  
		    "$ns#receiveMO", 
		    'rpc',                        
		    'encoded',                    
		    'get MO'      
		    );

function receiveMO($commandcode,$serviceid,$userid,$message,$requestid,$username,$password){
return new soapval('return','xsd:string',getMO($commandcode,$serviceid,$userid,$message,$requestid,$username,$password));
}

if ( !isset( $HTTP_RAW_POST_DATA ) ) $HTTP_RAW_POST_DATA =file_get_contents( 'php://input' );
$server->service($HTTP_RAW_POST_DATA); 

function getMO($commandcode,$serviceid,$userid,$message,$requestid,$username,$password) {
	if($username == "south" && $password=="south.123!@#"){
		//$con = mysql_connect("localhost","root","");
		$con = mysql_connect("localhost","phamios_adstore","3xV}f=^6AWn8");
		if (!$con)
		  {
		  	die('Could not connect: ' . mysql_error());
		  }else{ 
			//mysql_select_db("store8x", $con); 
			mysql_select_db("phamios_store8x", $con); 
			date_default_timezone_set('Asia/Bangkok');
			$timenow = date('d-m-Y h:i:s A');
			
			$sql = "SELECT COUNT(*) AS total FROM tbmos WHERE requestId ='".$requestid."'";
			$query = mysql_query($sql);
			$row = mysql_fetch_assoc($query);
			$total = $row['total'];
			
			if($total==1) {
				return "-1";
			} else {
				  $query2= "INSERT INTO tbmos(dest, phone, content,created,telco,cmdcode,requestId) VALUES('".$serviceid."','".$userid."','".$message."','".$timenow."','".null."','".$commandcode."','".$requestid."')";
				  if (!mysql_query($query2,$con))
				  {
				  	 mysql_close($con);
				  	 return "-1";
				  }else{
				  	mysql_close($con);
					return $userid.' da nap them 15 xu vao tai khoan de xem phim thanh cong !';
				  }
			}   
			
		  }
	}else {
		return "-1";
	}
} 


?>
